import aclUserApi from '@/api/aclUser'
import { Message } from 'element-ui'

const state = {
    userList: [],
    total: 0,
}

const mutations = {
    SET_USERLIST(state, userList) {
        state.userList = userList
    },
    SET_TOTAL(state, total) {
        state.total = total
    },
}

const actions = {
    async getUserList({commit}, { page, limit, username, nickName }) {
        try {
            const result = await aclUserApi.getUserList(page, limit, username, nickName)
            if (result.code === 20000) {
                commit('SET_USERLIST', result.data.items)
                commit('SET_TOTAL', result.data.total)
            } else {
                Message.error('用户列表失败')
            }
        } catch (error) {
            Message.error('请求获取用户列表失败')
        }
    },
    async addUser({ commit }, userInfo) {
        try {
            const result = await aclUserApi.addUser(userInfo)
            if (result.code === 20000) {
                return 'ok'
            } else {
                return Promise.reject(new Error('falied'))
            }
        } catch (error) {
            return Promise.reject(new Error(error.Message))
        }
    },
    async updateUser({ commit }, userInfo) {
        try {
            const result = await aclUserApi.updateUser(userInfo)
            if (result.code === 20000) {
                return 'ok'
            } else {
                return Promise.reject(new Error('falied'))
            }
        } catch (error) {
            return Promise.reject(new Error(error.Message))
        }
    },
    
}

const getters = {
    
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}
